package com.jphoebe.plugin.mybatis.service.setting;

import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.jphoebe.plugin.mybatis.bean.setting.GlobalSetting;
import com.jphoebe.plugin.mybatis.bean.setting.ProjectSetting;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * GlobalSettingService class
 *
 * @author 蒋时华
 * @date 2019/2/19
 */
@State(name = "JPMybatisProjectSetting",
        storages = @Storage(value = "JPMybatis/JPMybatis-ProjectSetting.xml")
)
public class ProjectSettingService implements PersistentStateComponent<ProjectSetting> {

    private ProjectSetting projectSetting;

    public static ProjectSettingService getInstance(@NotNull Project project) {
        return ServiceManager.getService(project, ProjectSettingService.class);
    }

    public ProjectSettingService() {
        this.projectSetting = new ProjectSetting();
    }

    @Nullable
    @Override
    public ProjectSetting getState() {
        if (projectSetting == null) {
            projectSetting = new ProjectSetting();
        }
        return projectSetting;
    }

    @Override
    public void loadState(@NotNull ProjectSetting state) {
        XmlSerializerUtil.copyBean(state, projectSetting);
    }

}
